<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>使用mysql-proxy 快速实现mysql 集群读写分离 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>使用mysql-proxy 快速实现mysql 集群读写分离</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目前较为常见的mysql读写分离分为两种： </p>
<ol>
<li><p>基于程序代码内部实现：在代码中对select操作分发到从库；其它操作由主库执行；这类方法也是目前生产环境应用最广泛，知名的如DISCUZ X2。优点是性能较好，因为在程序代码中实现，不需要增加额外的设备作为硬件开支。缺点是需要开发人员来实现，运维人员无从下手。 </p>
</li>
<li><p>基于中间代理层实现：我们都知道代理一般是位于客户端和服务器之间，代理服务器接到客户端请求后通过判断然后转发到后端数据库。在这有两个代表性程序</p>
</li>
</ol>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/%E4%BD%BF%E7%94%A8mysql-proxy%20%E5%BF%AB%E9%80%9F%E5%AE%9E%E7%8E%B0mysql%20%E9%9B%86%E7%BE%A4%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB/01.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;mysql-proxy：mysql-proxy为mysql开源项目，通过其自带的lua脚本进行sql判断，虽然是mysql官方产品，但是mysql官方并不建议将mysql-proxy用到生产环境。  </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;amoeba：由陈思儒开发，作者曾就职于阿里巴巴，现就职于盛大。该程序由java语言进行开发，目前只听说阿里巴巴将其用于生产环境。另外，此项目严重缺少维护和推广（作者有个官方博客，很多用户反馈的问题发现作者不理睬） </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;经过上述简单的比较，通过程序代码实现mysql读写分离自然是一个不错的选择。但是并不是所有的应用都适合在程序代码中实现读写分离，像大型SNS、B2C这类应用可以在代码中实现，因为这样对程序代码本身改动较小；像一些大型复杂的java应用，这种类型的应用在代码中实现对代码改动就较大了。所以，像这种应用一般就会考虑使用代理层来实现。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面我们看一下如何搭建mysql-proxy来实现mysql读写分离 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;环境拓扑如下： </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关于mysql、mysql主从的搭建，在此不再演示，如下的操作均在mysql-proxy（192.168.1.200）服务器进行 </p>
<h2 id="一、安装mysql-proxy"><a href="#一、安装mysql-proxy" class="headerlink" title="一、安装mysql-proxy"></a>一、安装mysql-proxy</h2><h3 id="1、安装lua-mysql-proxy需要使用lua脚本进行数据转发"><a href="#1、安装lua-mysql-proxy需要使用lua脚本进行数据转发" class="headerlink" title="1、安装lua  (mysql-proxy需要使用lua脚本进行数据转发)"></a>1、安装lua  (mysql-proxy需要使用lua脚本进行数据转发)</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">tar zxvf lua-5.1.4.tar.gz </div><div class="line"><span class="built_in">cd</span> lua-5.1.4</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;vi Makefile，修改INSTALL_TOP= /usr/local/lua </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">make posix </div><div class="line">make install</div></pre></td></tr></table></figure>
<h3 id="2、安装libevent"><a href="#2、安装libevent" class="headerlink" title="2、安装libevent"></a>2、安装libevent</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">tar zxvf libevent-2.0.8-rc.tar.gz </div><div class="line"><span class="built_in">cd</span> libevent-2.0.8-rc </div><div class="line">./configure --prefix=/usr/<span class="built_in">local</span>/libevent </div><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
<h3 id="3、安装check"><a href="#3、安装check" class="headerlink" title="3、安装check"></a>3、安装check</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">tar zxvf check-0.9.8.tar.gz </div><div class="line"><span class="built_in">cd</span> check-0.9.8 </div><div class="line"></div><div class="line">./configure &amp;&amp; make &amp;&amp; make install</div></pre></td></tr></table></figure>
<h3 id="4、安装mysql客户端"><a href="#4、安装mysql客户端" class="headerlink" title="4、安装mysql客户端"></a>4、安装mysql客户端</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">tar zxvf mysql-5.0.92.tar.gz </div><div class="line"><span class="built_in">cd</span> mysql-5.0.92 </div><div class="line">./configure </div><div class="line">--without-server &amp;&amp; make &amp;&amp; make install</div></pre></td></tr></table></figure>
<h3 id="5、设置环境变量"><a href="#5、设置环境变量" class="headerlink" title="5、设置环境变量"></a>5、设置环境变量</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;（安装mysql-proxy所需变量） </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">vi /etc/profile </div><div class="line"></div><div class="line"><span class="built_in">export</span> </div><div class="line">LUA_CFLAGS=<span class="string">"-I/usr/local/lua/include"</span> LUA_LIBS=<span class="string">"-L/usr/local/lua/lib -llua -ldl"</span> </div><div class="line">LDFLAGS=<span class="string">"-L/usr/local/libevent/lib -lm"</span> </div><div class="line"><span class="built_in">export</span> </div><div class="line">CPPFLAGS=<span class="string">"-I/usr/local/libevent/include"</span> </div><div class="line"><span class="built_in">export</span> </div><div class="line">CFLAGS=<span class="string">"-I/usr/local/libevent/include"</span> </div><div class="line"><span class="comment"># source /etc/profile</span></div></pre></td></tr></table></figure>
<h3 id="6、安装mysql-proxy"><a href="#6、安装mysql-proxy" class="headerlink" title="6、安装mysql-proxy"></a>6、安装mysql-proxy</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">tar zxvf mysql-proxy-0.6.0.tar.gz </div><div class="line"><span class="built_in">cd</span> mysql-proxy-0.6.0 </div><div class="line"> ./configure --prefix=/usr/<span class="built_in">local</span>/mysql-proxy --with-mysql --with-lua </div><div class="line">make &amp;&amp; make install</div></pre></td></tr></table></figure>
<h3 id="7、启动mysql-proxy"><a href="#7、启动mysql-proxy" class="headerlink" title="7、启动mysql-proxy"></a>7、启动mysql-proxy</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本次对两台数据库实现了读写分离；mysql-master为可读可写，mysql-slave为只读 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/mysql-proxy/sbin/mysql-proxy </div><div class="line">--proxy-backend-addresses=192.168.1.201:3306 </div><div class="line">--proxy-read-only-backend-addresses=192.168.1.202:3306 </div><div class="line">--proxy-lua-script=/usr/<span class="built_in">local</span>/mysql-proxy/share/mysql-proxy/rw-splitting.lua </div><div class="line">&amp;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：如果正常情况下启动后终端不会有任何提示信息，mysql-proxy启动后会启动两个端口4040和4041，4040用于SQL转发，4041用于管理mysql-proxy。如有多个mysql-slave可以依次在后面添加 </p>
<h2 id="二、测试"><a href="#二、测试" class="headerlink" title="二、测试"></a>二、测试</h2><h3 id="1、连接测试"><a href="#1、连接测试" class="headerlink" title="1、连接测试"></a>1、连接测试</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为默认情况下mysql数据库不允许用户在远程连接 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt;grant </div><div class="line">all privileges on *.* to identified by <span class="string">'123456'</span>; </div><div class="line">mysql&gt;flush privileges;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;客户端连接 </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#mysql -uroot -p123456 -h192.168.1.200 -P4040</span></div></pre></td></tr></table></figure>
<h3 id="2、读写分离测试"><a href="#2、读写分离测试" class="headerlink" title="2、读写分离测试"></a>2、读写分离测试</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;为了测试出mysql读写分离的真实性，在测试之前，需要开启两台mysql的log功能，然后在mysql-slave服务器停止复制 </p>
<ul>
<li>在两台mysql配置文件my.cnf中加入log=query.log，然后重启</li>
<li>在mysql-slave上执行SQL语句stop slave</li>
<li>在两台mysql上执行#tail -f /usr/local/mysql/var/query.log</li>
<li>在客户端上连接mysql（三个连接以上），然后执行create、select等SQL语句，观察两台mysql的日志有何变化</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注：生产环境中除了进行程序调试外，其它不要开启mysql查询日志，因为查询日志记录了客户端的所有语句，频繁的IO操作将会导致mysql整体性能下降 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;总结：在上述环境中，mysql-proxy和mysql-master、mysql-slave三台服务器均存在单点故障。如果在可用性要求较高的场合，单点隐患是绝对不允许的。为了避免mysql-proxy单点隐患有两种方法，一种方法是mysql-proxy配合keepalived做双机，另一种方法是将mysql-proxy和应用服务安装到同一台服务器上；为了避免mysql-master单点故障可以使用DRBD+heartbear做双机；避免mysql-slave单点故障增加多台mysql-slave即可，因为mysql-proxy会自动屏蔽后端发生故障的mysql-slave。</p>
<h2 id="附-mysql-proxy-LUA-读写分离脚本代码"><a href="#附-mysql-proxy-LUA-读写分离脚本代码" class="headerlink" title="附: mysql-proxy LUA 读写分离脚本代码:"></a>附: mysql-proxy LUA 读写分离脚本代码:</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div><div class="line">208</div><div class="line">209</div><div class="line">210</div><div class="line">211</div><div class="line">212</div><div class="line">213</div><div class="line">214</div><div class="line">215</div><div class="line">216</div><div class="line">217</div><div class="line">218</div><div class="line">219</div><div class="line">220</div><div class="line">221</div><div class="line">222</div><div class="line">223</div><div class="line">224</div><div class="line">225</div><div class="line">226</div><div class="line">227</div><div class="line">228</div><div class="line">229</div><div class="line">230</div><div class="line">231</div><div class="line">232</div><div class="line">233</div><div class="line">234</div><div class="line">235</div><div class="line">236</div><div class="line">237</div><div class="line">238</div><div class="line">239</div><div class="line">240</div><div class="line">241</div><div class="line">242</div><div class="line">243</div><div class="line">244</div><div class="line">245</div><div class="line">246</div><div class="line">247</div><div class="line">248</div><div class="line">249</div><div class="line">250</div><div class="line">251</div><div class="line">252</div><div class="line">253</div><div class="line">254</div><div class="line">255</div><div class="line">256</div><div class="line">257</div><div class="line">258</div><div class="line">259</div><div class="line">260</div><div class="line">261</div><div class="line">262</div><div class="line">263</div><div class="line">264</div><div class="line">265</div><div class="line">266</div><div class="line">267</div><div class="line">268</div><div class="line">269</div><div class="line">270</div><div class="line">271</div><div class="line">272</div><div class="line">273</div><div class="line">274</div><div class="line">275</div><div class="line">276</div><div class="line">277</div><div class="line">278</div><div class="line">279</div><div class="line">280</div><div class="line">281</div><div class="line">282</div><div class="line">283</div><div class="line">284</div><div class="line">285</div><div class="line">286</div><div class="line">287</div><div class="line">288</div><div class="line">289</div><div class="line">290</div><div class="line">291</div><div class="line">292</div><div class="line">293</div><div class="line">294</div><div class="line">295</div><div class="line">296</div><div class="line">297</div><div class="line">298</div><div class="line">299</div><div class="line">300</div><div class="line">301</div><div class="line">302</div><div class="line">303</div><div class="line">304</div><div class="line">305</div><div class="line">306</div><div class="line">307</div><div class="line">308</div><div class="line">309</div><div class="line">310</div><div class="line">311</div></pre></td><td class="code"><pre><div class="line">--[[</div><div class="line">--</div><div class="line">-- author : KDr2 </div><div class="line">-- version 0.01</div><div class="line">-- SYNOPSIS:</div><div class="line">---  </div><div class="line">1.维护了一个连接池</div><div class="line">---  2.读写分离，简单的将select开头的语句放到slave上执行</div><div class="line">---  </div><div class="line">3.事务支持，所有事务放到master上执行，事务中不更改连接</div><div class="line">---  4.简单日志</div><div class="line">--</div><div class="line">--]]</div><div class="line"></div><div class="line">--- config vars</div><div class="line"><span class="built_in">local</span> min_idle_connections = 4</div><div class="line"><span class="built_in">local</span> </div><div class="line">max_idle_connections = 8</div><div class="line"><span class="built_in">local</span> log_level=1</div><div class="line"><span class="built_in">local</span> encoding=<span class="string">"utf8"</span></div><div class="line">--- </div><div class="line">end of config</div><div class="line"></div><div class="line"></div><div class="line">事务标识，在事务内不归还连接</div><div class="line"></div><div class="line"><span class="built_in">local</span> </div><div class="line">transaction_flags=&#123;&#125;</div><div class="line">setmetatable(transaction_flags,&#123;__index=<span class="keyword">function</span>() </div><div class="line"><span class="built_in">return</span> 0 end&#125;)</div><div class="line"></div><div class="line">-- <span class="built_in">log</span> system</div><div class="line"><span class="built_in">log</span>=&#123;</div><div class="line">   level=&#123;debug=1,info=2,warn=3,error=4&#125;,</div><div class="line">   </div><div class="line">funcs=&#123;<span class="string">"debug"</span>,<span class="string">"info"</span>,<span class="string">"warn"</span>,<span class="string">"error"</span>&#125;,</div><div class="line">&#125;</div><div class="line"><span class="keyword">function</span> log.log(level,m)</div><div class="line">   </div><div class="line"><span class="keyword">if</span> level &gt;= log_level <span class="keyword">then</span></div><div class="line">      <span class="built_in">local</span> msg=<span class="string">"["</span> .. os.date(<span class="string">"%Y-%m-%d %X"</span>) </div><div class="line">..<span class="string">"] "</span>.. log.funcs[level] .. <span class="string">": "</span> .. tostring(m)</div><div class="line">      <span class="built_in">print</span>(msg) -- TODO  </div><div class="line">write msg into a <span class="built_in">log</span> file.</div><div class="line">   end</div><div class="line">end</div><div class="line"><span class="keyword">for</span> i,v <span class="keyword">in</span> ipairs(log.funcs) </div><div class="line"><span class="keyword">do</span></div><div class="line">   <span class="built_in">log</span>[v]=<span class="keyword">function</span>(m) log.log(log.level[v],m) end</div><div class="line">end</div><div class="line"></div><div class="line">-- connect to server</div><div class="line"><span class="keyword">function</span> connect_server() </div><div class="line">   log.info(<span class="string">" starting </span></div><div class="line"><span class="string">connect_server ... "</span>)</div><div class="line">   <span class="built_in">local</span> least_idle_conns_ndx = 0</div><div class="line">   <span class="built_in">local</span> </div><div class="line">least_idle_conns = 0</div><div class="line">   </div><div class="line">   <span class="keyword">for</span> i = 1, <span class="comment">#proxy.backends do</span></div><div class="line">      <span class="built_in">local</span> s </div><div class="line">= proxy.backends[i]</div><div class="line">      <span class="built_in">local</span> pool = s.pool </div><div class="line">      <span class="built_in">local</span> cur_idle = </div><div class="line">pool.users[<span class="string">""</span>].cur_idle_connections</div><div class="line"></div><div class="line">      log.debug(<span class="string">"["</span>.. s.address ..<span class="string">"].connected_clients = "</span> .. </div><div class="line">s.connected_clients)</div><div class="line">      log.debug(<span class="string">"["</span>.. s.address ..<span class="string">"].idling_connections </span></div><div class="line"><span class="string">= "</span> .. cur_idle)</div><div class="line">      log.debug(<span class="string">"["</span>.. s.address ..<span class="string">"].type = "</span> .. </div><div class="line">s.type)</div><div class="line">      log.debug(<span class="string">"["</span>.. s.address ..<span class="string">"].state = "</span> .. s.state)</div><div class="line"></div><div class="line">      <span class="keyword">if</span> s.state ~= proxy.BACKEND_STATE_DOWN <span class="keyword">then</span></div><div class="line">         -- try to </div><div class="line">connect to each backend once at least</div><div class="line">         <span class="keyword">if</span> cur_idle == 0 </div><div class="line"><span class="keyword">then</span></div><div class="line">            proxy.connection.backend_ndx = i</div><div class="line">            </div><div class="line">log.info(<span class="string">"server ["</span>.. proxy.backends[i].address ..<span class="string">"] open new </span></div><div class="line"><span class="string">connection"</span>)</div><div class="line">            <span class="built_in">return</span></div><div class="line">         end</div><div class="line">         -- try to open at </div><div class="line">least min_idle_connections</div><div class="line">         <span class="keyword">if</span> least_idle_conns_ndx == 0 </div><div class="line">or</div><div class="line">            ( cur_idle &lt; min_idle_connections and </div><div class="line">              </div><div class="line">cur_idle &lt; least_idle_conns ) <span class="keyword">then</span></div><div class="line">            least_idle_conns_ndx = </div><div class="line">i</div><div class="line">            least_idle_conns = cur_idle</div><div class="line">         end</div><div class="line">      end</div><div class="line">   </div><div class="line">end</div><div class="line"></div><div class="line">   <span class="keyword">if</span> least_idle_conns_ndx &gt; 0 <span class="keyword">then</span></div><div class="line">      proxy.connection.backend_ndx </div><div class="line">= least_idle_conns_ndx</div><div class="line">   end</div><div class="line">   </div><div class="line">   <span class="keyword">if</span> proxy.connection.backend_ndx </div><div class="line">&gt; 0 <span class="keyword">then</span> </div><div class="line">      <span class="built_in">local</span> s = </div><div class="line">proxy.backends[proxy.connection.backend_ndx]</div><div class="line">      <span class="built_in">local</span> pool = s.pool </div><div class="line"></div><div class="line">      <span class="built_in">local</span> cur_idle = pool.users[<span class="string">""</span>].cur_idle_connections</div><div class="line"></div><div class="line">      <span class="keyword">if</span> cur_idle &gt;= min_idle_connections <span class="keyword">then</span></div><div class="line">         -- we have 4 </div><div class="line">idling connections <span class="keyword">in</span> the pool, that<span class="string">'s good enough</span></div><div class="line"><span class="string">         log.debug("using </span></div><div class="line"><span class="string">pooled connection from: " .. proxy.connection.backend_ndx)</span></div><div class="line"><span class="string">         return </span></div><div class="line"><span class="string">proxy.PROXY_IGNORE_RESULT</span></div><div class="line"><span class="string">      end</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">   -- open a new connection </span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">   log.info("opening new connection on: " .. </span></div><div class="line"><span class="string">proxy.backends[proxy.connection.backend_ndx].address)</span></div><div class="line"><span class="string">end</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">---</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">-- auth.packet is the packet</span></div><div class="line"><span class="string">function read_auth_result( auth )</span></div><div class="line"><span class="string">   if </span></div><div class="line"><span class="string">auth.packet:byte() == proxy.MYSQLD_PACKET_OK then</span></div><div class="line"><span class="string">      -- 连接正常</span></div><div class="line"><span class="string">      </span></div><div class="line"><span class="string">proxy.connection.backend_ndx = 0</span></div><div class="line"><span class="string">   elseif auth.packet:byte() == </span></div><div class="line"><span class="string">proxy.MYSQLD_PACKET_EOF then</span></div><div class="line"><span class="string">      -- we received either a </span></div><div class="line"><span class="string">      -- * </span></div><div class="line"><span class="string">MYSQLD_PACKET_ERR and the auth failed or</span></div><div class="line"><span class="string">      -- * MYSQLD_PACKET_EOF which </span></div><div class="line"><span class="string">means a OLD PASSWORD (4.0) was sent</span></div><div class="line"><span class="string">      log.error("(read_auth_result) ... </span></div><div class="line"><span class="string">not ok yet");</span></div><div class="line"><span class="string">   elseif auth.packet:byte() == proxy.MYSQLD_PACKET_ERR </span></div><div class="line"><span class="string">then</span></div><div class="line"><span class="string">      log.error("auth failed!")</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">end</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">--- </span></div><div class="line"><span class="string">-- read/write splitting</span></div><div class="line"><span class="string">function read_query( packet ) </span></div><div class="line"><span class="string">   </span></div><div class="line"><span class="string">log.debug("[read_query]")</span></div><div class="line"><span class="string">   log.debug("authed backend = " .. </span></div><div class="line"><span class="string">proxy.connection.backend_ndx)</span></div><div class="line"><span class="string">   log.debug("used db = " .. </span></div><div class="line"><span class="string">proxy.connection.client.default_db)</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">   if packet:byte() == proxy.COM_QUIT then</span></div><div class="line"><span class="string">      proxy.response = </span></div><div class="line"><span class="string">&#123;</span></div><div class="line"><span class="string">         type = proxy.MYSQLD_PACKET_OK,</span></div><div class="line"><span class="string">      &#125;</span></div><div class="line"><span class="string">      return </span></div><div class="line"><span class="string">proxy.PROXY_SEND_RESULT</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">   if proxy.connection.backend_ndx == 0 then</span></div><div class="line"><span class="string">      local </span></div><div class="line"><span class="string">is_read=(string.upper(packet:sub(2))):match("^SELECT")</span></div><div class="line"><span class="string">      local </span></div><div class="line"><span class="string">target_type=proxy.BACKEND_TYPE_RW</span></div><div class="line"><span class="string">      if is_read then </span></div><div class="line"><span class="string">target_type=proxy.BACKEND_TYPE_RO end</span></div><div class="line"><span class="string">      for i = 1, #proxy.backends </span></div><div class="line"><span class="string">do</span></div><div class="line"><span class="string">         local s = proxy.backends[i]</span></div><div class="line"><span class="string">         local pool = s.pool </span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">         local cur_idle = </span></div><div class="line"><span class="string">pool.users[proxy.connection.client.username].cur_idle_connections</span></div><div class="line"><span class="string">         </span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">         if cur_idle &gt; 0 and </span></div><div class="line"><span class="string">            s.state ~= </span></div><div class="line"><span class="string">proxy.BACKEND_STATE_DOWN and </span></div><div class="line"><span class="string">            s.type == target_type </span></div><div class="line"><span class="string">then</span></div><div class="line"><span class="string">            proxy.connection.backend_ndx = i</span></div><div class="line"><span class="string">            </span></div><div class="line"><span class="string">break</span></div><div class="line"><span class="string">         end</span></div><div class="line"><span class="string">      end</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">   -- sync the client-side </span></div><div class="line"><span class="string">default_db with the server-side default_db</span></div><div class="line"><span class="string">   if proxy.connection.server and </span></div><div class="line"><span class="string">proxy.connection.client.default_db ~= proxy.connection.server.default_db </span></div><div class="line"><span class="string">then</span></div><div class="line"><span class="string">      local server_db=proxy.connection.server.default_db</span></div><div class="line"><span class="string">      local </span></div><div class="line"><span class="string">client_db=proxy.connection.client.default_db</span></div><div class="line"><span class="string">      local default_db= </span></div><div class="line"><span class="string">(#client_db &gt; 0) and client_db or server_db</span></div><div class="line"><span class="string">      if #default_db &gt; 0 </span></div><div class="line"><span class="string">then</span></div><div class="line"><span class="string">         proxy.queries:append(2, string.char(proxy.COM_INIT_DB) .. </span></div><div class="line"><span class="string">default_db)</span></div><div class="line"><span class="string">         proxy.queries:append(2, string.char(proxy.COM_QUERY) .. </span></div><div class="line"><span class="string">"set names '</span><span class="string">" .. encoding .."</span><span class="string">'")</span></div><div class="line"><span class="string">         log.info("change database to " .. </span></div><div class="line"><span class="string">default_db);</span></div><div class="line"><span class="string">      end</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">   if proxy.connection.backend_ndx &gt; 0 </span></div><div class="line"><span class="string">then</span></div><div class="line"><span class="string">      log.debug("Query[" .. packet:sub(2) .. "] Target is [" .. </span></div><div class="line"><span class="string">proxy.backends[proxy.connection.backend_ndx].address .."]")</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">   </span></div><div class="line"><span class="string">proxy.queries:append(1, packet)</span></div><div class="line"><span class="string">   return proxy.PROXY_SEND_QUERY</span></div><div class="line"><span class="string">end</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">---</span></div><div class="line"><span class="string">-- as long as we are in a transaction keep the connection</span></div><div class="line"><span class="string">-- </span></div><div class="line"><span class="string">otherwise release it so another client can use it</span></div><div class="line"><span class="string">function read_query_result( </span></div><div class="line"><span class="string">inj ) </span></div><div class="line"><span class="string">   local res      = assert(inj.resultset)</span></div><div class="line"><span class="string">   local flags    = </span></div><div class="line"><span class="string">res.flags</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">   if inj.id ~= 1 then</span></div><div class="line"><span class="string">      -- ignore the result of the USE </span></div><div class="line"><span class="string">&lt;default_db&gt;</span></div><div class="line"><span class="string">      return proxy.PROXY_IGNORE_RESULT</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">   </span></div><div class="line"><span class="string">is_in_transaction = flags.in_trans</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">   if flags.in_trans then</span></div><div class="line"><span class="string">      </span></div><div class="line"><span class="string">transaction_flags[proxy.connection.server.thread_id] = </span></div><div class="line"><span class="string">transaction_flags[proxy.connection.server.thread_id] + 1</span></div><div class="line"><span class="string">   elseif </span></div><div class="line"><span class="string">inj.query:sub(2):lower():match("^%s*commit%s*$") or </span></div><div class="line"><span class="string">inj.query:sub(2):lower():match("^%s*rollback%s*$") then</span></div><div class="line"><span class="string">      </span></div><div class="line"><span class="string">transaction_flags[proxy.connection.server.thread_id] = </span></div><div class="line"><span class="string">transaction_flags[proxy.connection.server.thread_id] - 1</span></div><div class="line"><span class="string">      if </span></div><div class="line"><span class="string">transaction_flags[proxy.connection.server.thread_id] &lt; 0 then </span></div><div class="line"><span class="string">transaction_flags[proxy.connection.server.thread_id] = 0 end</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">   </span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">   log.debug("transaction res : " .. </span></div><div class="line"><span class="string">tostring(transaction_flags[proxy.connection.server.thread_id]));</span></div><div class="line"><span class="string">   if </span></div><div class="line"><span class="string">transaction_flags[proxy.connection.server.thread_id]==0 or </span></div><div class="line"><span class="string">transaction_flags[proxy.connection.server.thread_id] == nil then </span></div><div class="line"><span class="string">      -- </span></div><div class="line"><span class="string">isnot in a transaction, need to release the backend</span></div><div class="line"><span class="string">      </span></div><div class="line"><span class="string">proxy.connection.backend_ndx = 0</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">end</span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">--- </span></div><div class="line"><span class="string">-- close the connections if we have enough connections in the </span></div><div class="line"><span class="string">pool</span></div><div class="line"><span class="string">--</span></div><div class="line"><span class="string">-- @return nil - close connection </span></div><div class="line"><span class="string">-- IGNORE_RESULT - store </span></div><div class="line"><span class="string">connection in the pool</span></div><div class="line"><span class="string">function disconnect_client()</span></div><div class="line"><span class="string">   </span></div><div class="line"><span class="string">log.debug("[disconnect_client]")</span></div><div class="line"><span class="string">   if proxy.connection.backend_ndx == 0 </span></div><div class="line"><span class="string">then</span></div><div class="line"><span class="string">      for i = 1, #proxy.backends do</span></div><div class="line"><span class="string">         local s = </span></div><div class="line"><span class="string">proxy.backends[i]</span></div><div class="line"><span class="string">         local pool = s.pool </span></div><div class="line"><span class="string">         local cur_idle = </span></div><div class="line"><span class="string">pool.users[proxy.connection.client.username].cur_idle_connections</span></div><div class="line"><span class="string">         </span></div><div class="line"><span class="string"></span></div><div class="line"><span class="string">         if s.state ~= proxy.BACKEND_STATE_DOWN and</span></div><div class="line"><span class="string">            cur_idle </span></div><div class="line"><span class="string">&gt; max_idle_connections then</span></div><div class="line"><span class="string">            -- try to disconnect a </span></div><div class="line"><span class="string">backend</span></div><div class="line"><span class="string">            proxy.connection.backend_ndx = i</span></div><div class="line"><span class="string">            </span></div><div class="line"><span class="string">log.info("[".. proxy.backends[i].address .."] closing connection, idling: " .. </span></div><div class="line"><span class="string">cur_idle)</span></div><div class="line"><span class="string">            return</span></div><div class="line"><span class="string">         end</span></div><div class="line"><span class="string">      end</span></div><div class="line"><span class="string">      return </span></div><div class="line"><span class="string">proxy.PROXY_IGNORE_RESULT</span></div><div class="line"><span class="string">   end</span></div><div class="line"><span class="string">end</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/36. 使用mysql-proxy 快速实现mysql 集群读写分离/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
